﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class FireHydrantCla : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				UIApplication application = commandData.Application;
                Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
				Document document = application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
                Autodesk.Revit.DB.View activeView = document.ActiveView;
				Selection selection = activeUIDocument.Selection;
				transaction = new Transaction(document, "消火栓计算");
				transaction.Start();
				IList<Element> list = new List<Element>();
				ISelectionFilter selectionFilter = new FireSelectionFilter();
				for (;;)
				{
					list = selection.PickElementsByRectangle(selectionFilter, "请框选要计算的消火栓");
					if (list.Count > 0)
					{
						break;
					}
					MessageBox.Show("请框选要计算的消火栓！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				}
				new FrmCalFireHydant(list, document).ShowDialog();
				transaction.Commit();
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && transaction.GetStatus() != (TransactionStatus)3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
